package web;



import service.impl.ProductServiceImpl;
import ssm.pojo.PageBean;
import ssm.pojo.Product;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/product")
public class ProductServlet extends BeanUser {
    /**
     * 通过id寻找商品
     * @param request
     * @param response
     * @throws IOException
     */

    public void findProductByID(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            String pid = request.getParameter("pid");
            ProductServiceImpl ps = new ProductServiceImpl();
            Product product = ps.findPById(pid);
            request.setAttribute("product",product);
            request.getRequestDispatcher("/jsp/product_info.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }



    public void listPr(HttpServletRequest request, HttpServletResponse response){
        try {
            //当前类别id
            Object cid = request.getParameter("cid");
            String s = String.valueOf(cid);
            //当前页数
            String num = request.getParameter("number");
            //每页显示条数
            String tot = request.getParameter("total");
            if(num==null||num.equals("")){
                num="1";
            }
            if(tot==null||tot.equals("")){
                tot="12";
            }
            int number = Integer.parseInt(num);
            int total = Integer.parseInt(tot);

            ProductServiceImpl ps = new ProductServiceImpl();
            PageBean<Product> productPage = ps.CateByProduct(number, total, s);
            request.setAttribute("productPage",productPage);
            request.getRequestDispatcher("/jsp/product_list.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
